$(function () {
    // 添加自己得js
    // 显示图书模块
    function getBooks() {
        $.ajax({
            type: 'get',
            url: 'http://www.liulongbin.top:3006/api/getbooks',
            success: function (res) {
                let str = ''
                res.data.forEach(item => {
                    str += `<tr>
                    <td class="index">${item.id}</td>
                    <td>${item.bookname}</td>
                    <td>${item.author}</td>
                    <td>${item.publisher}</td>
                    <td><button type="button" class="btn btn-danger">删除</button></td>
                </tr>`
                });
                $('tbody').html(str)

                // 删除图书模块
                $('.btn-danger').click(function () {
                    let index = $(this).parent().siblings('.index')[0].innerText
                    let x = confirm('确定删除吗？？？')
                    if (x === false) return
                    $.ajax({
                        url: 'http://www.liulongbin.top:3006/api/delbook',
                        data: {
                            id: index
                        },
                        success: function (res) {
                            // alert('确认删除吗？？？')
                            alert(res.msg)
                            getBooks()
                        }
                    })
                })
            }
        })
    }
    getBooks()
    // 添加图书模块
    $('#submit').click(() => {
        let data = {
            bookname: $("#bookname").val(),
            author: $("#author").val(),
            publisher: $("#publisher").val()
        }
        $.ajax({
            type: 'post',
            url: 'http://www.liulongbin.top:3006/api/addbook',
            data: data,
            success: function (res) {
                alert(res.msg)
                getBooks()
                console.log(res);
                if (res.status === 201) {
                    $("#bookname").val('')
                    $("#author").val('')
                    $("#publisher").val('')
                }

            }
        })
    })
    // 刷新按钮
    $('#refresh').click(function () {
        getBooks()
    })
})